package com.wolf.etl.controller;

import com.wolf.etl.model.EtlDataSourceModel;
import com.wolf.etl.service.IEtlDataSourceService;
import com.wolf.exception.WolfException;
import com.wolf.provider.exception.ERROR_MSG;
import com.wolf.provider.order.info.PageBean;
import com.wolf.provider.util.JsonUtil;
import com.wolf.provider.util.StringUtils;
import com.wolf.system.model.SysUser;
import com.wolf.system.service.ISysUserService;
import com.wolf.vo.PageQueryParam;
import com.wolf.vo.PageResult;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * @author sdyang
 * @date 2019/10/2 17:18
 **/
@RestController
@RequestMapping("/etl/datasource")
public class EtlDataSourceController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(EtlDataSourceController.class);

    @Autowired
    private IEtlDataSourceService manager;

    /**
     * 跳转列表页面
     *
     * @return
     */
    @RequestMapping(value = "/list")
    public ModelAndView sysDictView() {
        ModelAndView model = new ModelAndView();
        model.setViewName("etl/etl_datasource");
        model.addObject("currentUrl", "/etl/datasource/list");
        SysUser sysUser = userService.getSessionSysUser();
        model.addObject("sysUser", sysUser);
        model.addObject("currentTitle", "数据源");
        return model;
    }

    /**
     * 显示页面数据信息
     *
     * @return
     */
    @RequestMapping(value = "/pagelist")
    public PageResult sysDictInfoData(PageQueryParam queryParam) {

        PageBean pageBean = new PageBean();
        pageBean.setStart(queryParam.getStart());
        pageBean.setPageSize(queryParam.getLength());

        EtlDataSourceModel model = new EtlDataSourceModel();

        if (StringUtils.isNotEmpty(queryParam.getQueryData())) {
            JSONObject jsonParams = JSONObject.fromObject(queryParam.getQueryData());
            if (jsonParams.has("name")) {
                model.setName(jsonParams.get("name").toString());
            }
            if (jsonParams.has("dialect")) {
                model.setDialect(jsonParams.get("dialect").toString());
            }
            if (jsonParams.has("status")) {
                if(StringUtils.isNotEmpty(jsonParams.get("status").toString())) {
                    model.setStatus(Integer.parseInt(jsonParams.get("status").toString()));
                }
            }
        }

        PageResult result = manager.findByPage(model, pageBean);
        result.setDraw(queryParam.getDraw());

        return result;
    }


    /**
     * 保存数据
     *
     * @param requestData
     * @return
     */
    @PostMapping(value = "/save")
    public String saveData(@RequestParam(value = "requestData") String requestData) {
        if (StringUtils.isEmpty(requestData)) {
            WolfException.report(ERROR_MSG.ERROR_STRING_001);
        }
        SysUser sysUser = userService.getSessionSysUser();
        EtlDataSourceModel model = JsonUtil.parseObject(requestData, EtlDataSourceModel.class);
        if (model != null && model.getId() != null) {
            model.setModified_person(sysUser.getUserName());
            model.setModified_time(new Date());
            boolean isResult = manager.updateModel(model);
            if (isResult) {
                return SUCCESS_MSG;
            }
            return FAIL_MSG;
        }
        model.setCreator(sysUser.getUserName()
        );
        model.setCreate_time(new Date());
        boolean isResult = manager.insertModel(model);

        if (isResult) {
            return SUCCESS_MSG;
        }
        return FAIL_MSG;
    }

    /**
     * 删除数据
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/delete")
    public String deleteData(@RequestParam(value = "id") Long id) {
        EtlDataSourceModel model = new EtlDataSourceModel();
        model.setId(id);
        boolean isResult = manager.deleteModel(model);
        if (isResult) {
            return SUCCESS_MSG;
        }
        return FAIL_MSG;
    }
}
